package com.yy.mydota;

import java.util.ArrayList;

import com.yy.adapter.MyFragmentPageAdapter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 资料库界面
 * @author liyuye
 * @date 2015-5-20 上午11:44:41
 */
public class DatasFragment extends Fragment implements OnPageChangeListener {
	private Fragment herosFragment,articlesFragment;
	private ViewPager vp_datas;
	private TextView texv_heros,texv_articles;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fra_datas_view, container,false);
		
		texv_heros = (TextView) getActivity().findViewById(R.id.texv_heros);
		texv_articles = (TextView) getActivity().findViewById(R.id.texv_articles);
		texv_heros.setOnClickListener(textClick);
		texv_articles.setOnClickListener(textClick);
		
		
		herosFragment = new HerosFragment();
		articlesFragment = new ArticlesFragment();
		final ArrayList<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(herosFragment);
		fragments.add(articlesFragment);
		FragmentPagerAdapter fraAdapter = new MyFragmentPageAdapter(
				getChildFragmentManager(), fragments);
		vp_datas = (ViewPager) view.findViewById(R.id.vp_datas);
		vp_datas.setAdapter(fraAdapter);
		vp_datas.setOnPageChangeListener(this);
		
		
		return view;
	}
	
	OnClickListener textClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.texv_heros:
				texv_heros.setBackgroundResource(R.drawable.ziliaokunavs_rev);
				texv_articles.setBackgroundResource(R.drawable.ziliaokunavu);
				texv_heros.setTextColor(getResources().getColor(R.color.red));
				texv_articles.setTextColor(getResources().getColor(R.color.orange));
				vp_datas.setCurrentItem(0);
				break;
			case R.id.texv_articles:
				texv_heros.setBackgroundResource(R.drawable.ziliaokunavu_rev);
				texv_articles.setBackgroundResource(R.drawable.ziliaokunavs);
				texv_articles.setTextColor(getResources().getColor(R.color.red));
				texv_heros.setTextColor(getResources().getColor(R.color.orange));
				vp_datas.setCurrentItem(1);
				break;
			}
			
		}
	};

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		switch(arg0){
		case 0:
			texv_heros.setBackgroundResource(R.drawable.ziliaokunavs_rev);
			texv_articles.setBackgroundResource(R.drawable.ziliaokunavu);
			texv_heros.setTextColor(getResources().getColor(R.color.red));
			texv_articles.setTextColor(getResources().getColor(R.color.orange));
			break;
		case 1:
			texv_heros.setBackgroundResource(R.drawable.ziliaokunavu_rev);
			texv_articles.setBackgroundResource(R.drawable.ziliaokunavs);
			texv_articles.setTextColor(getResources().getColor(R.color.red));
			texv_heros.setTextColor(getResources().getColor(R.color.orange));
			break;
		}
		
	}
}
